package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.jackson.JsonUtil;
import cn.signalpha.core.tool.utils.*;
import cn.signalpha.modules.shop.dto.SellerDTO;
import cn.signalpha.modules.shop.entity.Seller;
import cn.signalpha.modules.shop.entity.SellerAddress;
import cn.signalpha.modules.shop.entity.SellerCategory;
import cn.signalpha.modules.shop.service.*;
import cn.signalpha.modules.shop.vo.SellerDeliveryConfigVO;
import cn.signalpha.modules.shop.vo.SellerVO;
import cn.signalpha.modules.shop.wrapper.SellerWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;


@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/seller")
@Api(value = "商户", tags = "商户")
public class SpiSellerController extends BaseController {

	private final SellerService sellerService;
	private final SellerApplyService sellerApplyService;
	private final CityDeliveryService cityDeliveryService;
	private final SellerAddressService sellerAddressService;
	private final SellerCategoryService sellerCategoryService;

	@GetMapping("/info")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "商户信息", notes = "")
	public R<SellerVO> info() {
		Seller seller = sellerService.getById(getUser().getTenantId());
		SellerVO sellerVO = SellerWrapper.build().entityVO(seller);
		if(sellerVO.getServerRatioSw() == 0){
			SellerCategory sellerCategory = sellerCategoryService.getById(sellerVO.getCategoryId());
			sellerVO.setServerRatio(sellerCategory.getServerRatio());
		}
		return R.data(sellerVO);
	}

	@PostMapping("/update")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "编辑商户信息", notes = "")
	public R update(@Valid @RequestBody SellerDTO.SellerUpdateDTO sellerDTO) {
		sellerDTO.setId(Long.valueOf(getUser().getTenantId()));
		sellerDTO.setExt(JsonUtil.toJson(sellerDTO.getExtVO()));

		Seller seller = new Seller();
		seller.setId(sellerDTO.getId());
		seller.setUpdateInfo(JsonUtil.toJson(sellerDTO));
		seller.setUpdateRemark("您提交的信息正在审核中，请耐心等待审核");
		sellerService.updateById(seller);
		sellerService.removeCacheById(sellerDTO.getId());
		return R.status(true);
	}

	@PostMapping("/update/pick")
	@ApiOperationSupport(order = 20)
	@ApiOperation(value = "商户自提配置", notes = "")
	public R updatePick(@Valid @RequestBody SellerDTO.SellerUpdatePickDTO sellerDTO) {
		Long sellerId = Long.valueOf(getUser().getTenantId());
		Seller seller = new Seller();
		seller.setId(sellerId);
		seller.setIsOpenPick(sellerDTO.getIsOpenPick());
		seller.setPickConfig(JsonUtil.toJson(sellerDTO.getSellerPickConfigList()));
		sellerService.updateById(seller);
		sellerService.removeCacheById(sellerDTO.getId());
		return R.status(true);
	}

	@PostMapping("/update/delivery")
	@ApiOperationSupport(order = 20)
	@Transactional(rollbackFor = Exception.class)
	@ApiOperation(value = "商户配送配置", notes = "")
	public R updateDelivery(@Valid @RequestBody SellerDTO.SellerUpdateDeliveryDTO sellerDTO) {
		Seller sellerUpdate = new Seller();
		sellerUpdate.setId(Long.valueOf(getUser().getTenantId()));
		sellerUpdate.setIsOpenDelivery(sellerDTO.getIsOpenDelivery());
		if(sellerDTO.getIsOpenDelivery() == 1){
			SellerAddress sellerAddress = sellerAddressService.getById(sellerDTO.getSellerDeliveryConfig().getAddressId());
			Assert.isNull(sellerAddress, "商户地址不存在");
			//判断是新增还是修改
			Seller seller = sellerService.getById(sellerUpdate.getId());
			Map<String, Object> resultMap;
			SellerDeliveryConfigVO sellerDeliveryConfig = JsonUtil.parse(seller.getDeliveryConfig(), SellerDeliveryConfigVO.class);
			if(ObjectUtil.isEmpty(sellerDeliveryConfig) || StringUtil.isBlank(sellerDeliveryConfig.getShopId())){
				resultMap = cityDeliveryService.createSeller(sellerUpdate.getId().toString(),
					seller.getName() + "-" + sellerAddress.getName(),
					seller.getMobile(),
					sellerAddress.getMobile(),
					sellerAddress.getAddress(),
					sellerAddress.getLng(),
					sellerAddress.getLat()
				);
				sellerDTO.getSellerDeliveryConfig().setShopId(ObjectUtil.isNotEmpty(resultMap.get("shop_id")) ? String.valueOf(resultMap.get("shop_id")) : StringPool.EMPTY);
				sellerDTO.getSellerDeliveryConfig().setOpenId(ObjectUtil.isNotEmpty(resultMap.get("open_id")) ? String.valueOf(resultMap.get("open_id")) : StringPool.EMPTY);
			}else {
//				resultMap = cityDeliveryService.updateSeller(sellerDeliveryConfig.getShopId(),
//					seller.getName() + "-" + sellerAddress.getName(),
//					sellerAddress.getMobile(),
//					sellerAddress.getAddress(),
//					sellerAddress.getLng(),
//					sellerAddress.getLat()
//				);
			}
//			sellerDTO.getSellerDeliveryConfig().setShopId(ObjectUtil.isNotEmpty(resultMap.get("shop_id")) ? String.valueOf(resultMap.get("shop_id")) : StringPool.EMPTY);
//			sellerDTO.getSellerDeliveryConfig().setOpenId(ObjectUtil.isNotEmpty(resultMap.get("open_id")) ? String.valueOf(resultMap.get("open_id")) : StringPool.EMPTY);
//			sellerUpdate.setDeliveryConfig(JsonUtil.toJson(sellerDTO.getSellerDeliveryConfig()));
		}
		sellerUpdate.setDeliveryConfig(JsonUtil.toJson(sellerDTO.getSellerDeliveryConfig()));
		sellerService.updateById(sellerUpdate);
		sellerService.removeCacheById(sellerDTO.getId());
		return R.status(true);
	}

	@PostMapping("/update/password")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "修改密码", notes = "")
	public R updatePassword(@Valid @RequestBody SellerDTO.updatePasswordDTO sellerDTO) {
		sellerDTO.setId(getUser().getUserId());
		sellerDTO.setUpdateTime(new Date());
		sellerDTO.setPassword(DigestUtil.encrypt(sellerDTO.getPassword()));
		sellerService.removeCacheById(sellerDTO.getId());
		return R.status(sellerService.updateById(BeanUtil.copy(sellerDTO, Seller.class)));
	}

}
